using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using GF.ResourcesLoad;

//UI界面栈管理器
public class UIMgr : MonoBehaviour
{
    private static Stack<GameObject> stack = new Stack<GameObject>();

    public static GameObject ShowView(string name)
    {
        if (stack.Count > 0)
        {
            stack.Peek().SetActive(false);
        }
        GameObject go = PopView(name);
        stack.Push(go);
        return go;
    }

    public static GameObject Pop3DView(string name)
    {
#if LCS
        GameObject go = Instantiate(AppFacade.Instance.ResMgr.Load("FCG/Prefabs/" + name) as GameObject);
#else
        GameObject go = Instantiate(CResourceManager.Load<GameObject>("FCG/Prefabs/" + name));
#endif
        go.transform.localPosition = new Vector3(0, 0, 0);
        go.transform.localEulerAngles = new Vector3(0, 0, 0);
        go.SetActive(true);
        return go;
    }

    public static GameObject PopView(string name, bool belowGuidePanel = true)
    {
#if LCS
        GameObject go = Instantiate(AppFacade.Instance.ResMgr.Load("FCG/Prefabs/" + name) as GameObject, GameObject.Find("Canvas").transform);
#else
        GameObject go = Instantiate(CResourceManager.Load<GameObject>("FCG/Prefabs/" + name), GameObject.Find("Canvas").transform);
#endif
        go.transform.localPosition = new Vector3(0, 0, 0);
        go.transform.localEulerAngles = new Vector3(0, 0, 0);
        go.SetActive(true);

        if (belowGuidePanel)
        {
            GameObject guide = GameObject.Find("UIGuide(Clone)");
            if (guide != null)
            {
                guide.transform.SetSiblingIndex(go.transform.GetSiblingIndex() + 1);
            }
        }
        return go;
    }
    public static GameObject CreateItem(string name, Transform parent)
    {
        GameObject go = PopView(name, false);
        go.transform.SetParent(parent);
        go.transform.localScale = new Vector3(1, 1, 1);
        go.transform.localPosition = new Vector3(0, 0, 0);
        go.transform.localEulerAngles = new Vector3(0, 0, 0);
        return go;
    }
    public static void CloseView()
    {
        if (stack.Count > 1)
        {
            GameObject go = stack.Pop();
            Object.Destroy(go);

        }
        if (stack.Count > 0)
        {
            stack.Peek().SetActive(true);
        }
    }

    public static GameObject GetView(string name)
    {
        if (stack.Count > 1)
        {
            foreach (var item in stack)
            {
                if (item.name.Equals(name))
                {
                    return item;
                }
            }
        }
        return null;
    }

    public static void OpenScene(string SceneName)
    {
        stack.Clear();
#if LCS
        AppFacade.Instance.ABMgr.LoadSync("Scenes/" + SceneName);
#endif
        SceneManager.LoadScene(SceneName, LoadSceneMode.Single);
    }
    public static void Clear(Transform parent)
    {
        for (int i = parent.childCount - 1; i >= 0; i--)
        {
            Destroy(parent.GetChild(i).gameObject);
        }
    }
    public static void Clear(Transform parent, int ignore)
    {
        for (int i = parent.childCount - 1; i >= ignore; i--)
        {
            DestroyImmediate(parent.GetChild(i).gameObject);
        }
    }
}
